package cn.waka.sys.admin.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Node;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.service.NodeService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/node")
public class NodeController extends BaseController {

	@Autowired
	private NodeService nodeService;

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}
	
	@RequestMapping(path = "/index",name="节点列表", produces = "text/html; charset=UTF-8")
	public ModelAndView getNodeList(HttpServletRequest request,HttpServletResponse response) {
		List<Node> nodes=nodeService.getNodeList();
		ModelAndView mv=new ModelAndView();
		mv.addObject("nodes",nodes); 
		return mv; 
	}
	
	@RequestMapping(path = "/select_pnode",name="获取父节点", produces = "text/html; charset=UTF-8")
	public ModelAndView getPNodes(HttpServletRequest request,HttpServletResponse response) {
		String idStr=request.getParameter("id");
		int id=0;
		Node node = new Node();
		if(idStr!=null&&Validator.isNum(idStr)){
			id=Integer.parseInt(idStr);
			node = nodeService.getNodeById(id);
		}
		List<Node> nodes=nodeService.getNodeList();
		ModelAndView mv=new ModelAndView();
		mv.addObject("nodes",nodes);
		mv.addObject("pnode",nodeService.getNodeById(node.getPid()));
		mv.addObject("node",node);
		return mv;
	}
	
	@RequestMapping(path = "/form",name="添加节点", produces = "text/html; charset=UTF-8")
	public ModelAndView form(HttpServletRequest request,HttpServletResponse response) {
		List<Node> nodes=nodeService.getNodeList();
		ModelAndView mv=new ModelAndView();
		mv.addObject("nodes",nodes);
		return mv;
	}
	
	@RequestMapping(path = "/save",name="保存节点", produces = "application/json; charset=UTF-8")
	public Result save(HttpServletRequest req, HttpServletResponse res){
		String pidStr=req.getParameter("pid");
		int pid=0;
		Node node = new Node();
		if(pidStr!=null&&Validator.isNum(pidStr)){
			pid=Integer.parseInt(pidStr);
			node.setPid(pid);
		}
		String name=req.getParameter("node");
		String title=req.getParameter("title");
		node.setNode(name);
		node.setTitle(title);
		node.setCreate_at(new Timestamp(System.currentTimeMillis()));
		int rs = nodeService.save(node);
		if(rs>0){
			return error(1,"添加成功！");
		}else{
			return error(0,"添加失败或已存在该节点！");
		}
	}
	
	@RequestMapping(path = "/edit",name="修改节点", produces = "application/json; charset=UTF-8")
	public Result edit(HttpServletRequest req, HttpServletResponse res){
		String idStr=req.getParameter("data-id");
		int id=0;
		Node node = new Node();
		if(idStr!=null&&Validator.isNum(idStr)){
			id=Integer.parseInt(idStr);
			node = nodeService.getNodeById(id);
		}
		String pidStr=req.getParameter("pid");
		int pid=0;
		if(pidStr!=null&&Validator.isNum(pidStr)){
			pid=Integer.parseInt(pidStr);
			node.setPid(pid);
		}
		String name=req.getParameter("name");
		String valueStr=req.getParameter("value");
		int value=0;
		if(valueStr!=null&&Validator.isNum(valueStr)){
			value=Integer.parseInt(valueStr);
		}
		if(name!=null){
			if(name.equals("is_auth")){
				node.setAuth(value==1?true:false);
			}else if(name.equals("is_menu")){
				node.setMenu(value==1?true:false);
			}else if(name.equals("title")){
				node.setTitle(valueStr);
			}
		}
		int rs = nodeService.update(node);
		
		if(rs>0){
			return error(1,"修改成功！");
		}else{
			return error(0,"修改失败！");
		}
	}
	
	
	
}
